package com.taysear.parcel.fieldfactory;

import com.taysear.parcel.ParcelApplication;
import com.taysear.parcel.domain.Station;
import com.taysear.parcel.util.Constants;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.addon.jpacontainer.fieldfactory.FieldFactory;
import com.vaadin.data.Item;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;

public class BusFieldFactory extends FieldFactory {
	
	ParcelApplication app;
	public final TextField busId = new TextField("Bus id");
	final TextField registrationNumber = new TextField("Registration number");
	final TextField make = new TextField("Make");
	final TextField model = new TextField("Model");
	final TextField tonnage = new TextField("Tonnage");
	final TextField nextService = new TextField("Next service");
	final TextField millage = new TextField("Millage");
	final TextField lastfuel = new TextField("Lastfuel");
	final TextField chasisNumber = new TextField("Chasis number");
	final TextField available = new TextField("Available");
	final TextField conditionDescription = new TextField("Condition description");
	final DateField availableDate = new DateField("Date available");
	final DateField conditionDescriptionDate = new DateField("Date condition description");
	NativeSelect fromStation = new NativeSelect("From station");
	NativeSelect toStation = new NativeSelect("To station");

	public BusFieldFactory(ParcelApplication app) {
		this.app = app;
	}

	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f = null;
		if ("busId".equals(propertyId)) {
			busId.setRequired(true);
			busId.setWidth(Constants.COMMON_FIELD_WIDTH);
			return busId;
		} if ("registrationNumber".equals(propertyId)) {
			registrationNumber.setRequired(true);
			registrationNumber.setWidth(Constants.COMMON_FIELD_WIDTH);
			registrationNumber.setRequiredError(registrationNumber.getCaption()+"is required");
			registrationNumber.setNullRepresentation("");
			return registrationNumber;
		} if ("make".equals(propertyId)) {
			make.setWidth(Constants.COMMON_FIELD_WIDTH);
			make.setNullRepresentation("");
			return make;
		} if ("model".equals(propertyId)) {
			model.setWidth(Constants.COMMON_FIELD_WIDTH);
			model.setNullRepresentation("");
			return model;
		} if ("tonnage".equals(propertyId)) {
			tonnage.setWidth(Constants.COMMON_FIELD_WIDTH);
			tonnage.setNullRepresentation("");
			return tonnage;
		} if ("nextService".equals(propertyId)) {
			nextService.setWidth(Constants.COMMON_FIELD_WIDTH);
			nextService.setNullRepresentation("");
			nextService.addValidator(new IntegerValidator("Only numbers allowed"));
			return nextService;
		} if ("lastfuel".equals(propertyId)) {
			lastfuel.setWidth(Constants.COMMON_FIELD_WIDTH);
			lastfuel.setNullRepresentation("");
			lastfuel.addValidator(new IntegerValidator("Only numbers allowed"));
			return lastfuel;
		} if ("millage".equals(propertyId)) {
			millage.setWidth(Constants.COMMON_FIELD_WIDTH);
			millage.setNullRepresentation("");
			millage.addValidator(new IntegerValidator("Only numbers allowed"));
			return millage;
		} if ("chasisNumber".equals(propertyId)) {
			chasisNumber.setWidth(Constants.COMMON_FIELD_WIDTH);
			chasisNumber.setNullRepresentation("");
			return chasisNumber;
		} if ("available".equals(propertyId)) {
			available.setWidth(Constants.COMMON_FIELD_WIDTH);
			available.setNullRepresentation("");
			return available;
		} if ("conditionDescription".equals(propertyId)) {
			conditionDescription.setWidth(Constants.COMMON_FIELD_WIDTH);
			conditionDescription.setNullRepresentation("");
			return conditionDescription;
		} if ("availableDate".equals(propertyId)) {
			availableDate.setWidth(Constants.COMMON_FIELD_WIDTH);
			availableDate.setDateFormat(Constants.DATE_FORMAT_STRING);
			availableDate.setResolution(DateField.RESOLUTION_DAY);
			return availableDate;
		} if ("conditionDescriptionDate".equals(propertyId)) {
			conditionDescriptionDate.setWidth(Constants.COMMON_FIELD_WIDTH);
			conditionDescriptionDate.setDateFormat(Constants.DATE_FORMAT_STRING);
			conditionDescriptionDate.setResolution(DateField.RESOLUTION_DAY);
			return conditionDescriptionDate;
		} if ("fromStation".equals(propertyId)) {
			try{
				fromStation = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Station> container = JPAContainerFactory.make(Station.class, app.PERSISTENCE_UNIT);
				fromStation.setContainerDataSource(container);
			}
			fromStation.setWidth(Constants.COMMON_FIELD_WIDTH);
			fromStation.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			return fromStation;
		} if ("toStation".equals(propertyId)) {
			try{
				toStation = (NativeSelect) super.createField(item, propertyId, uiContext);
			} catch (ClassCastException e) {
				JPAContainer<Station> container = JPAContainerFactory.make(Station.class, app.PERSISTENCE_UNIT);
				toStation.setContainerDataSource(container);
			}
			toStation.setWidth(Constants.COMMON_FIELD_WIDTH);
			toStation.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_ITEM);
			return toStation;
		} else {
			// Use the super class to create a suitable field base on the
			// property type.
			f = super.createField(item, propertyId, uiContext);
		}
		return f;
	}
}
